package com.alex.demo.service;


import com.alex.demo.db1.dao.XADB1Mapper;
import com.alex.demo.db1.model.XADB1;
import com.alex.demo.db2.dao.XADB2Mapper;
import com.alex.demo.db2.model.XADB2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class XAService {

    @Resource
    private XADB1Mapper XADB1Mapper;

    @Resource
    private XADB2Mapper XADB2Mapper;

    // 事务管理器配置
    @Transactional(transactionManager = "xaTransaction")
    public void testXA() {
        XADB1 XADB1 = new XADB1();
        XADB1.setId(2);
        XADB1.setName("xa_db1");
        XADB1Mapper.insert(XADB1);
        // // 在此处会抛异常，两条事务都会回滚
        int i = 1/0;
        XADB2 XADB2 = new XADB2();
        XADB2.setId(2);
        XADB2.setName("xa_db2");
        XADB2Mapper.insert(XADB2);
    }
}
